package com.amani.util;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class Utils {

	/**
	 * 把一个字符串（yyyy-MM-dd）转化成Date
	 * 
	 * @param Date
	 *            date_str;
	 * @return String
	 */
	public static Date getDateByStr(String str, String format) {
		Date date = new Date();
		try {
			java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
					format);
			date = sdf.parse(str);
		} catch (Exception e) {
			System.out.println("String to Date error" + e.getMessage());
			date = null;
		}
		return date;
	}
	
	/**
	 * 把一个字符串转换成timestamp
	 * @param timestampStr
	 * @return
	 */
	public static Timestamp stringToTimestamp(String timestampStr) {
		return stringToTimestamp(timestampStr, "yyyy-MM-dd HH:mm");
	}

	/**
	 * 把一个字符串转换成timestamp
	 * @param timestampStr
	 * @param format
	 * @return
	 */
	public static Timestamp stringToTimestamp(String timestampStr, String format) {
		if (timestampStr == null || timestampStr.trim().equals(" ")) {
			return null;
		}
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat(format);
			Date date = dateFormat.parse(timestampStr);
			return new Timestamp(date.getTime());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return null;
	}
	
//	public static double getDistance(double lat1, double lng1, double lat2,
//			double lng2) {
//		double radLat1 = rad(lat1);
//		double radLat2 = rad(lat2);
//		double a = radLat1 - radLat2;
//		double b = rad(lng1) - rad(lng2);
//		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
//				+ Math.cos(radLat1) * Math.cos(radLat2)
//				* Math.pow(Math.sin(b / 2), 2)));
//		s = s * EARTH_RADIUS;
//		s = Math.round(s * 10000) / 10000;
//		return s;
//	}
//
//	private static double rad(double d) {
//		return d * Math.PI / 180.0;
//	}

//	private static final double EARTH_RADIUS = 6378.137;
	
//	/**
//	 * 两个地理位置的间距
//	 * 返回单位暂未处理
//	 * @param wd1
//	 * @param jd1
//	 * @param wd2
//	 * @param jd2
//	 * @return
//	 */
//	public static double D_jw(double wd1, double jd1, double wd2, double jd2) {
//		double x, y, out;
//		double PI = 3.14159265;
//		double R = 6.371229 * 1e6;
//		x = (jd2 - jd1) * PI * R * Math.cos(((wd1 + wd2) / 2) * PI / 180) / 180;
//		y = (wd2 - wd1) * PI * R / 180;
//		out = Math.hypot(x, y);
//		return out / 1e6;
//	}
	
	/**
	 * 根据用户ID和商家ID生成新卡卡号
	 * @param appuserid
	 * @param unitid
	 * @return
	 */
	public static String genNewCardNumber(String unitno,int appuserid,int unitid) {
		String number = unitno+"8";
		String str = ""+appuserid;
		int len = str.length();
		int dif = 8-len;
		Random x = new Random() ;
		for(int i=0; i < dif; i++) {
			int t = x.nextInt(26);
			number+=EN.charAt(t);
		}
		number+=appuserid;
		return number;
	}
	
	private static final String EN= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
	
	/**
	 * double转str并格式化
	 * @param d
	 * @return
	 */
	public static String doubleToStr(double d) {
		DecimalFormat df = new DecimalFormat("0.#");
		String str = df.format(d);
		return str;
	}
}
